var types = [];

window.onload = function() {
	if (!-[1, ] && !window.XMLHttpRequest || navigator.userAgent.indexOf("MSIE 8.0") > 0) {
		document.onfocusout = function() { return true; }
	} else {
		window.onblur = function() { return true; }
	}
}

$(function() {
	$.fn.raty.defaults.path = RS.webPath + '/static/zcchs/pingfen/lib/img';
	$("#star1").raty({
		score: 0,
		click: function(score, evt) {
			var id = $(this).attr('id');
			var score = score
		}
	});
	$("#star2").raty({
		score: 0,
		click: function(score, evt) {
			var id = $(this).attr('id');
			var score = score
		}
	});
	$("#star3").raty({
		score: 0,
		click: function(score, evt) {
			var id = $(this).attr('id');
			var score = score
		}
	});
	$("#star4").raty({
		score: 0,
		click: function(score, evt) {
			var id = $(this).attr('id');
			var score = score
		}
	});
	$("#star5").raty({
		score: 0,
		click: function(score, evt) {
			var id = $(this).attr('id');
			var score = score
		}
	});
	$("#star6").raty({
		score: 0,
		click: function(score, evt) {
			var id = $(this).attr('id');
			var score = score
		}
	});
	showFen();
	if (userstarcount != 0) {
		$("#pingfenbuttonshow").hide();
		$("#pingfenbuttonhide").show()
	} else {
		$("#pingfenbuttonshow").show();
		$("#pingfenbuttonhide").hide()
	}
});

function printLog(time) {
	var integerNowTime = parseInt(time);
	if (parseInt(integerNowTime) - parseInt(startT) >= 10) {
		startT = integerNowTime;
		$.ajax({
			url: RS.webPath + "/index/play/saveTime",
			data: "courseId=" + courseId + "&userId=" + userId + "&time=" + integerNowTime,
			type: "post",
			dataType: "json",
			async: true,
			success: function(re) {},
			error: function() {}
		})
	}
}
$("#submitStart").click(function() {
	pingjia(types)
});

function auto_submit() {
	player.videoPause()
}
$("body").blur(auto_submit);
window.onblur = auto_submit;

function showFen() {
	$.ajax({
		url: RS.webPath + "/index/play/getPfAvg",
		data: "courseId=" + courseId,
		type: "post",
		dataType: "json",
		async: false,
		success: function(re) {
			types = [];
			for (var i = 0; i < re.length; i++) {
				var num = re[i].STARAVG;
				var type = re[i].TYPE;
				types.push({
					"type": type
				});
				$("#" + type).raty({
					score: num,
					number: 5,
					click: function(score, evt) {
						var id = $(this).attr('id');
						var score = score;
						for (var i = 0; i < types.length; i++) {
							if (types[i].type == id) {
								types[i] = {
									"type": id,
									"score": score
								}
							}
						}
					}
				})
			}
		}
	})
}

function pingjia(data) {
	$.ajax({
		url: RS.webPath + "/index/play/pf",
		data: {
			"courseId": courseId,
			"data": JSON.stringify(data)
		},
		type: "post",
		dataType: "json",
		async: false,
		success: function(re) {
			if ("suc" == re.status) {
				layui.use('layer', function() {
					var layer = layui.layer;
					layer.msg('评价成功!')
				});
				showFen()
			} else if ("err" == re.status) {
				layui.use('layer', function() {
					var layer = layui.layer;
					layer.msg('你已评价该课程!')
				});
				showFen()
			} else if ("empty" == re.status) {
				if ("star1" == re.data) {
					layer.alert("\"课程与描述相符\"没有评价，请评价后再提交")
				} else if ("star2" == re.data) {
					layer.alert("\"老师的讲解表达\"没有评价，请评价后再提交")
				} else if ("star3" == re.data) {
					layer.alert("\"教学质量\"没有评价，请评价后再提交")
				} else if ("star4" == re.data) {
					layer.alert("\"视频清晰度\"没有评价，请评价后再提交")
				} else if ("star5" == re.data) {
					layer.alert("\"视频音效质量\"没有评价，请评价后再提交")
				} else if ("star6" == re.data) {
					layer.alert("\"课程实用性\"没有评价，请评价后再提交")
				}
			}
		},
		error: function() {
			layer.msg('系统错误请稍后再试！')
		}
	})
}